package cn.afactory.tygz.ui.base.views.subject;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.harwkin.nb.camera.BaseImgView;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import java.util.ArrayList;
import java.util.List;

import cn.afactory.tygz.R;
import cn.afactory.tygz.common.UserListType;
import cn.afactory.tygz.model.SnsCommentInfo;
import cn.afactory.tygz.model.SnsSubjectDetail;
import cn.afactory.tygz.ui.base.adapter.CommentListAdapter;
import cn.afactory.tygz.ui.base.adapter.NearByForumsAdapter;
import cn.afactory.tygz.ui.base.adapter.SubjectAgreeUserGirdAdapter;
import cn.afactory.tygz.ui.base.adapter.SubjectImgGirdAdapter;
import cn.afactory.tygz.ui.base.utils.DateUtil;
import cn.afactory.tygz.ui.base.utils.NaviUtils;
import cn.afactory.tygz.ui.base.utils.ScreenUtil;
import cn.afactory.tygz.ui.base.utils.StringUtils;
import cn.afactory.tygz.ui.base.utils.sns.ViewListener;
import cn.afactory.tygz.ui.base.utils.sns.ViewTools;
import cn.afactory.tygz.ui.base.views.AutoGridView;
import cn.afactory.tygz.ui.base.views.NoScrollListView;

/**
 * Created by Kenneth on 15/9/14.
 */
public class NearByForumItemView extends BaseSubjectView<SnsSubjectDetail, NearByForumItemView.ForumsHeaderViewHolder> {

    private Context mContext;
    private SubjectImgGirdAdapter mImgGirdAdapter;
    private CommentListAdapter mCommentListAdapter;
    private SubjectAgreeUserGirdAdapter mAgreeUserGirdAdapter;
    private int mImageWidth, mImageHeight;

    public NearByForumItemView(Context context) {
        super(context);
        mContext = context;
        mImageWidth = (ScreenUtil.getScreenWidth(mContext) - mContext.getResources().getDimensionPixelOffset(R.dimen.healthcircle_padding10) * 2
                - mContext.getResources().getDimensionPixelOffset(R.dimen.healthcircle_padding2) * 2) / 3;
        mImageHeight = mImageWidth + mContext.getResources().getDimensionPixelOffset(R.dimen.healthcircle_padding2);
    }

    @Override
    public View newView(LayoutInflater inflater, ViewGroup parent) {
        View view = inflater.inflate(R.layout.fg_near_by_forums_item, parent, false);
        return view;
    }

    NearByForumsAdapter.OnItemClickListener mItemClickListener;
    public void setOnItemClickLsn(NearByForumsAdapter.OnItemClickListener lsn){
        mItemClickListener = lsn;
    }

    @Override
    public ForumsHeaderViewHolder createViewHolder(View view) {
        final ForumsHeaderViewHolder viewHolder = new ForumsHeaderViewHolder();
        viewHolder.nearByForumsIcon = (ImageView)view.findViewById(R.id.nearby_forums_icon);
        viewHolder.nearByForumsName = (TextView)view.findViewById(R.id.nearby_forums_name);
        viewHolder.nearByForumsAge = (TextView)view.findViewById(R.id.nearby_forums_age);
        viewHolder.nearByForumsAgeRight = (View)view.findViewById(R.id.nearby_forums_age_right_view);
        viewHolder.nearByForumsCity = (TextView)view.findViewById(R.id.nearby_forums_city);
        viewHolder.nearByForumsTime = (TextView)view.findViewById(R.id.nearby_forums_time);
        viewHolder.nearByForumsCompany = (TextView)view.findViewById(R.id.nearby_forums_company);
        viewHolder.tv_tag_name = (TextView)view.findViewById(R.id.tv_tag_name);
        viewHolder.iv_tag = (ImageView) view.findViewById(R.id.iv_tag);
        viewHolder.ll_tag = (LinearLayout) view.findViewById(R.id.ll_tag);
        viewHolder.nearByForumsSex = (ImageView)view.findViewById(R.id.nearby_forums_sex);
        viewHolder.nearByForumsDistance = (TextView)view.findViewById(R.id.nearby_forums_distance);

        viewHolder.nearByForumsText = (TextView)view.findViewById(R.id.nearby_forums_text);
        viewHolder.nearByForumsPics = (AutoGridView)view.findViewById(R.id.nearby_forums_pic);
        viewHolder.nearByForumsTopic = (TextView)view.findViewById(R.id.nearby_forums_topic);
        viewHolder.nearByFormsFromTopicLayout = (RelativeLayout)view.findViewById(R.id.nearby_from_topic_layout);

        viewHolder.nearByForumsLikeCount = (TextView)view.findViewById(R.id.nearby_forums_like_count);
        viewHolder.nearByForumsLikePeople = (AutoGridView)view.findViewById(R.id.nearby_forums_like_list);
        viewHolder.nearByForumsComment = (TextView)view.findViewById(R.id.nearby_forums_comment_button);
        viewHolder.nearByForumsShare = (TextView)view.findViewById(R.id.nearby_forums_share_button);

        viewHolder.nearByFormsCommentHeader = (RelativeLayout)view.findViewById(R.id.nearby_forums_comment_header);
        viewHolder.nearByForumsCommentDivision = (View)view.findViewById(R.id.nearby_forums_comment_division);
        viewHolder.nearByForumsCommentCount = (TextView)view.findViewById(R.id.nearby_forums_comment_count);
        viewHolder.nearByForumsCommentMore = (LinearLayout)view.findViewById(R.id.nearby_forums_comment_more);
        viewHolder.nearByForumsCommentList = (NoScrollListView)view.findViewById(R.id.nearby_forums_comment_list);
        viewHolder.nearByForumsCommentList.setDivider(null);
        viewHolder.nearByForumsCommentList.setDividerHeight(0);

        return viewHolder;
    }

    @Override
    public View bindView(LayoutInflater mInflater, ViewGroup parent, final SnsSubjectDetail item, View convertView) {
        ForumsHeaderViewHolder viewHolder = null;
        if (convertView == null || !(convertView.getTag() instanceof ForumsHeaderViewHolder)) {
            convertView = newView(mInflater, parent);
            viewHolder = createViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ForumsHeaderViewHolder) convertView.getTag();
        }
        //bind data
        if (item.userIcon != null) {
            BaseImgView.loadimg(viewHolder.nearByForumsIcon,
                    item.userIcon,
                    R.drawable.ic_pic_loading_default_small,
                    R.drawable.ic_pic_loading_default_small,
                    R.drawable.ic_pic_loading_default_small,
                    ImageScaleType.EXACTLY,
                    (int) mContext.getResources().getDimension(R.dimen.healthcircle_padding30),
                    (int) mContext.getResources().getDimension(R.dimen.healthcircle_padding30),
                    20);
        } else {
            viewHolder.nearByForumsIcon.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_pic_loading_default_small));
        }
        viewHolder.nearByForumsIcon.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mItemClickListener) {
                    mItemClickListener.onClickIcon(item);
                }
            }
        });

        if (!TextUtils.isEmpty(item.nickName)) {
            viewHolder.nearByForumsName.setText(item.nickName);
            viewHolder.nearByForumsName.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != mItemClickListener) {
                        mItemClickListener.onClickIcon(item);
                    }
                }
            });
        } else {
            viewHolder.nearByForumsName.setText("");
        }

        if (!TextUtils.isEmpty(item.city) && item.birth != 0) {
            viewHolder.nearByForumsAgeRight.setVisibility(VISIBLE);
        } else {
            viewHolder.nearByForumsAgeRight.setVisibility(GONE);
        }

        if (item.birth != 0) {
            viewHolder.nearByForumsAge.setVisibility(VISIBLE);
            viewHolder.nearByForumsAge.setText(DateUtil.getAgeByNorth(item.birth) + "岁");
        } else {

            viewHolder.nearByForumsAge.setVisibility(GONE);
        }

        if (!TextUtils.isEmpty(item.city)) {
            viewHolder.nearByForumsCity.setVisibility(VISIBLE);
            viewHolder.nearByForumsCity.setText(item.city + "人");
        } else {
            viewHolder.nearByForumsCity.setVisibility(GONE);
        }

        if (item.addTime > 0) {
            viewHolder.nearByForumsTime.setText(DateUtil.getCreateAt(item.addTime));
        } else {
            viewHolder.nearByForumsTime.setText("");
        }

        if (!TextUtils.isEmpty(item.company)) {
            viewHolder.nearByForumsCompany.setVisibility(VISIBLE);
            viewHolder.nearByForumsCompany.setText(item.company);
            viewHolder.nearByForumsCompany.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != mItemClickListener) {
                        mItemClickListener.onClickCompany(item);
                    }
                }
            });
        } else {
            viewHolder.nearByForumsCompany.setVisibility(GONE);
        }

        if (item.sex == 0){
            viewHolder.nearByForumsSex.setImageResource(R.drawable.tag_male);
        } else {
            viewHolder.nearByForumsSex.setImageResource(R.drawable.tag_female);
        }
        if(item.distance > 0) {
            viewHolder.nearByForumsDistance.setVisibility(View.VISIBLE);
            viewHolder.nearByForumsDistance.setText("距离: " + StringUtils.formartDistance(item.distance));
        }else{
            viewHolder.nearByForumsDistance.setVisibility(View.INVISIBLE);
        }

        if (!TextUtils.isEmpty(item.content)) {
            viewHolder.nearByForumsText.setText(item.content.trim());
            viewHolder.nearByForumsText.setVisibility(View.VISIBLE);
            viewHolder.nearByForumsText.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != mItemClickListener) {
                        mItemClickListener.onClickTextContent(item);
                    }
                }
            });
        } else {
            viewHolder.nearByForumsText.setVisibility(View.GONE);
        }

        if (null != item.contentImg && item.contentImg.size() > 0) {
            viewHolder.nearByForumsPics.setVisibility(View.VISIBLE);
            mImgGirdAdapter = new SubjectImgGirdAdapter(mContext, item.contentImg, mImageWidth, mImageHeight);
            viewHolder.nearByForumsPics.setAdapter(mImgGirdAdapter);
            mImgGirdAdapter.setListener(new ViewListener() {
                @Override
                public void onClick(int position, List<String> mPath) {
                    mItemClickListener.onClickImage(position, mPath);
                }
            });
            viewHolder.nearByForumsPics.setFocusable(false);
            viewHolder.nearByForumsPics.setEnabled(false);
            viewHolder.nearByForumsPics.setPressed(false);
            viewHolder.nearByForumsPics.setClickable(false);
        }else{
            viewHolder.nearByForumsPics.setVisibility(View.GONE);
        }

        if (!TextUtils.isEmpty(item.topicName)) {
            viewHolder.nearByForumsTopic.setText("#"+item.topicName+"#");
            viewHolder.nearByFormsFromTopicLayout.setVisibility(View.VISIBLE);
            viewHolder.nearByForumsTopic.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    mItemClickListener.onClickTopic(item.topicName.trim(), item.topicId);
                }
            });
        } else {
            viewHolder.nearByFormsFromTopicLayout.setVisibility(View.GONE);
        }
        final AutoGridView autoGridView = viewHolder.nearByForumsLikePeople;
        ViewTools.setPraiseView(viewHolder.nearByForumsLikeCount, item.isAgree == 1, item.agree);
        viewHolder.nearByForumsLikeCount.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mItemClickListener.onPraise(autoGridView, (TextView) v, item);
            }
        });

        viewHolder.nearByForumsShare.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mItemClickListener.onClickShare(item);
            }
        });

        viewHolder.nearByForumsComment.setVisibility(VISIBLE);
        viewHolder.nearByForumsComment.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mItemClickListener.onClickComment(item);
            }
        });

        if (null != item.agreeUser && item.agreeUser.size() > 0) {
            int mIconWidth = mContext.getResources().getDimensionPixelOffset(R.dimen.agree_people_icon_width);
            int mIconHeight = mIconWidth + mContext.getResources().getDimensionPixelOffset(R.dimen.healthcircle_padding2);
            viewHolder.nearByForumsLikePeople.setVisibility(VISIBLE);
            mAgreeUserGirdAdapter = new SubjectAgreeUserGirdAdapter(mContext, item.agreeUser, mIconWidth, mIconHeight);
            viewHolder.nearByForumsLikePeople.setAdapter(mAgreeUserGirdAdapter);

            viewHolder.nearByForumsLikePeople.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    NaviUtils.gotoUserList(mContext,
                            -1,
                            null,
                            item.id,
                            item.agree,
                            UserListType.PRASIZE_LIST);
                }
            });
        } else {
            viewHolder.nearByForumsLikePeople.setVisibility(GONE);
        }

        if (item.comments > 0) {
            viewHolder.nearByForumsCommentCount.setVisibility(VISIBLE);
            viewHolder.nearByForumsCommentDivision.setVisibility(VISIBLE);
            viewHolder.nearByForumsCommentCount.setText("评论(" + item.comments + ")");
            viewHolder.nearByForumsCommentMore.setVisibility(VISIBLE);
            viewHolder.nearByForumsCommentMore.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != mItemClickListener) {
                        mItemClickListener.onClickTextContent(item);
                    }
                }
            });
            viewHolder.nearByFormsCommentHeader.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != mItemClickListener) {
                        mItemClickListener.onClickTextContent(item);
                    }
                }
            });
        } else {
            viewHolder.nearByForumsCommentCount.setVisibility(GONE);
            viewHolder.nearByForumsCommentDivision.setVisibility(GONE);
            viewHolder.nearByForumsCommentMore.setVisibility(GONE);
        }

        if (null != item.comment && item.comment.size() > 0) {
            viewHolder.nearByForumsCommentList.setVisibility(VISIBLE);
            if (item.comment.size() > 3) {
                List<SnsCommentInfo> commentInfoList = new ArrayList<SnsCommentInfo>();
                commentInfoList.add(item.comment.get(0));
                commentInfoList.add(item.comment.get(1));
                commentInfoList.add(item.comment.get(2));
                mCommentListAdapter = new CommentListAdapter(mContext, commentInfoList);
            } else {
                mCommentListAdapter = new CommentListAdapter(mContext, item.comment);
            }
            mCommentListAdapter.setOnItemClickLsn(mItemClickListener);
            viewHolder.nearByForumsCommentList.setAdapter(mCommentListAdapter);
            viewHolder.nearByForumsCommentList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    if (null != mItemClickListener) {
                        mItemClickListener.onClickTextContent(item);
                    }
                }
            });
        } else {
            viewHolder.nearByForumsCommentList.setVisibility(GONE);
        }
        if(item.tagType > 0){
            viewHolder.ll_tag.setVisibility(View.VISIBLE);
        }else {
            viewHolder.ll_tag.setVisibility(View.GONE);
        }
        cn.afactory.tygz.ui.base.utils.ViewUtils.applyTagView(item.tagType,viewHolder.iv_tag,viewHolder.ll_tag);
        if(item.tagList != null && item.tagList.size() > 0) {
            cn.afactory.tygz.ui.base.utils.ViewUtils.applyTagName(item.tagList,viewHolder.tv_tag_name);
        }

        return convertView;
    }


    public static class ForumsHeaderViewHolder extends BaseSubjectView.ViewHolder {
        ImageView nearByForumsIcon;
        TextView nearByForumsName;
        TextView nearByForumsAge;
        View nearByForumsAgeRight;
        TextView nearByForumsCity;
        TextView nearByForumsTime;
        TextView nearByForumsCompany;
        LinearLayout ll_tag;
        ImageView iv_tag;
        TextView tv_tag_name;
        ImageView nearByForumsSex;
        TextView nearByForumsDistance;

        TextView nearByForumsText;
        AutoGridView nearByForumsPics;
        TextView nearByForumsTopic;
        RelativeLayout nearByFormsFromTopicLayout;

        TextView nearByForumsLikeCount;
        AutoGridView nearByForumsLikePeople;
        TextView nearByForumsComment;
        TextView nearByForumsShare;

        RelativeLayout nearByFormsCommentHeader;
        TextView nearByForumsCommentCount;
        View nearByForumsCommentDivision;
        LinearLayout  nearByForumsCommentMore;
        NoScrollListView nearByForumsCommentList;
    }
}
